package web.action;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Product;
import model.SaleProduct;
import model.Shop;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.struts2.ServletActionContext;

import service.OrderManageService;
import util.MyProperties;


public class OrderManageAction {
	Logger logger = Logger.getLogger(this.getClass());
	private OrderManageService orderManageService;
	public String init(){
		
		return "init";
	}
	public String getClient(){
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			HttpServletRequest request = ServletActionContext.getRequest();
			JSONObject json = new JSONObject();
			String attribute = (String)request.getAttribute("queryStr");
			List<Shop> shops = this.orderManageService.getClients(attribute);
			JSONArray js = JSONArray.fromObject(shops);
			out.write(js.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public void save(){
		HttpServletRequest request =  ServletActionContext.getRequest();
		String prosInfo = (String)request.getParameter("prosInfo");
		String label = (String)request.getParameter("label");
		Integer client = Integer.parseInt((String)request.getParameter("client"));
		String orderDate = (String)request.getParameter("orderDate");
		JSONArray ja = JSONArray.fromObject(prosInfo);
		List<SaleProduct> salePros = (List<SaleProduct>)JSONArray.toCollection(ja, SaleProduct.class);
		
		try {
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			String serial = this.orderManageService.saveSaleOrder(orderDate,label,client,salePros);
			out.write(serial);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void downLoad(){
		HttpServletResponse response = ServletActionContext.getResponse();
		String serial = ServletActionContext.getRequest().getParameter("serial");
		FileInputStream fis = null;  
        OutputStream os = null;  
        
        try {  
        	Properties properties = MyProperties.getProperties("/sys.properties");
            fis = new FileInputStream(properties.getProperty("orderFilePath")+serial+".xls");  
          
            response.reset();
            response.addHeader("content-disposition", "attachment;filename=\""
					+ java.net.URLEncoder.encode(serial + ".xls", "UTF-8")
					+ "\"");
            response.setContentType("application/vnd.ms-excel; charset=GBK");
            os = response.getOutputStream();
            byte[] mybyte = new byte[8192];  
            int len = 0;  
            while ((len = fis.read(mybyte)) != -1) {  
                os.write(mybyte, 0, len);  
            }  
            os.close();  
        }catch (IOException e) {  
            e.printStackTrace();  
        }  
	}
	
	
	
	public Logger getLogger() {
		return logger;
	}
	public void setLogger(Logger logger) {
		this.logger = logger;
	}
	public OrderManageService getOrderManageService() {
		return orderManageService;
	}
	public void setOrderManageService(OrderManageService orderManageService) {
		this.orderManageService = orderManageService;
	}
	
}
